package com.tongzhan.controller;

import com.tongzhan.base.Return;
import com.tongzhan.entity.religion.Site;
import com.tongzhan.entity.religion.SiteInfo;
import com.tongzhan.service.SiteService;
import com.tongzhan.service.UserService;
import com.tongzhan.utils.RegexUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Slf4j
@RestController
@RequestMapping("/site")
public class SiteController {

    @Resource
    private SiteService siteService;

    @Resource
    private UserService userService;

    /**
     * 新增场所接口
     *
     * @param siteName 场所名称
     * @param religionCode 宗教编码
     * @param streetCode 街道编码
     * @param villageCode 村社区编码
     * @param address 详细地址
     * @param beliefType 信仰类型
     * @param buildYears 建造年代
     * @param historyEvolution 历史沿革
     * @param believers 主要供奉对象
     * @param picture 场所图片
     * @param personName 管理人员
     * @param personTelephone 联系方式
     * @param creditCode 统一社会信用代码
     * @param registrationNo 登记证号
     * @return Return
     */
    @PostMapping("/addSite")
    public Return addSite(String siteName, String religionCode, String streetCode, String villageCode,
                          String address, String beliefType, String buildYears, String historyEvolution,
                          String believers, String picture, String personName, String personTelephone, String creditCode, String registrationNo) {
        if (StringUtils.isEmpty(siteName) || "".equals(siteName.trim()) || StringUtils.isEmpty(religionCode)
                || StringUtils.isEmpty(streetCode) || StringUtils.isEmpty(villageCode)
                || StringUtils.isEmpty(address) || "".equals(address.trim())
                || StringUtils.isEmpty(creditCode) || "".equals(creditCode)
                || StringUtils.isEmpty(buildYears) || "".equals(buildYears.trim())
                || StringUtils.isEmpty(historyEvolution) || "".equals(historyEvolution.trim())
                || StringUtils.isEmpty(believers) || "".equals(believers) || StringUtils.isEmpty(picture)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (siteName.trim().length() > 100) {
            return new Return(Return.FAIL, "场所名称过长");
        }
        if (address.trim().length() > 500) {
            return new Return(Return.FAIL, "地址过长");
        }
        if (buildYears.trim().length() > 2000) {
            return new Return(Return.FAIL, "建造年代过长");
        }
        if (historyEvolution.trim().length() > 5000) {
            return new Return(Return.FAIL, "历史沿革过长");
        }
        if (believers.trim().length() > 100) {
            return new Return(Return.FAIL, "主要供奉对象过长");
        }
        if (StringUtils.isNotEmpty(personTelephone) && !RegexUtil.validPhoneNum("0", personTelephone)) {
            return new Return(Return.FAIL, "联系方式不正确");
        }
        //场所
        Site site = new Site();
        site.setSiteName(siteName.trim());
        site.setStreetCode(streetCode);
        site.setVillageCode(villageCode);
        site.setReligionCode(religionCode);
        return siteService.addSite(site, address.trim(), beliefType, buildYears,
                historyEvolution, believers.trim(), picture, personName, personTelephone, creditCode.trim(), registrationNo);
    }

    /**
     * 获取场所列表接口
     *
     * @param religionCode 宗教编码
     * @param page 页
     * @param size 大小
     * @return Return
     */
    @PostMapping("/getSiteList")
    public Return getSiteList(String religionCode, Integer page, @RequestParam(required = false, defaultValue = "10") Integer size) {
        if (page == null) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        if (page <= 0) {
            page = 1;
        }
        return siteService.getSiteList(religionCode, page, size);
    }

    /**
     * 场所详情接口
     *
     * @param siteInfoCode 场所信息编码
     * @return Return
     */
    @PostMapping("/siteDetails")
    public Return siteDetails(String siteInfoCode) {
        if (StringUtils.isEmpty(siteInfoCode)) {
            return new Return(Return.MISS_PARAM, "参数缺失");
        }
        return siteService.siteDetails(siteInfoCode);
    }

    /**
     * 上升、下降或置顶接口
     *
     * @param siteInfoCode 场所详情编码
     * @param type 类型
     * @return Return
     */
    @PostMapping("/changeSort")
    public Return changeSort(String siteInfoCode, Integer type) {
        if (StringUtils.isEmpty(siteInfoCode) || type == null) {
            return new Return(Return.FAIL, "参数缺失");
        }
        return siteService.changeSort(siteInfoCode, type);
    }

    /**
     * 场所信息编辑接口
     *
     * @param siteInfoCode 场所信息编码
     * @param siteName 场所名称
     * @param religionCode 宗教编码
     * @param personName 联系人
     * @param personTelephone 联系方式
     * @param creditCode 统一社会信用代码
     * @param registrationNo 登记证号
     * @param address 详细地址
     * @param beliefType 信仰类型
     * @param buildYears 建造年代
     * @param historyEvolution 历史沿革
     * @param believers 信奉对象
     * @param jurisdiction 是否受管辖
     * @param picture 图片
     * @param streetCode 街道编码
     * @param villageCode 村社区编码
     * @return Return
     */
    @PostMapping("/editSiteInfo")
    public Return editSiteInfo(String siteInfoCode, String siteName, String religionCode, String personName, String personTelephone,
                               String creditCode, String registrationNo, String address, String beliefType, String buildYears,
                               String historyEvolution, String believers, String jurisdiction, String picture, String streetCode, String villageCode) {
        if (StringUtils.isNotEmpty(personTelephone) && !RegexUtil.validPhoneNum("0", personTelephone)) {
            return new Return(Return.FAIL, "联系方式不正确");
        }
        //场所详情
        SiteInfo siteInfo = siteService.selectSiteInfoByCode(siteInfoCode);
        if (siteInfo == null) {
            return new Return(Return.FAIL, "场所不存在");
        }
        siteInfo.setSiteName(siteName);
        siteInfo.setReligionCode(religionCode);
        siteInfo.setPersonName(personName);
        siteInfo.setPersonTelephone(personTelephone);
        siteInfo.setCreditCode(creditCode);
        siteInfo.setRegistrationNo(registrationNo);
        siteInfo.setAddress(address);
        siteInfo.setBelievers(believers);
        siteInfo.setBeliefType(beliefType);
        siteInfo.setBuildYears(buildYears);
        siteInfo.setHistoryEvolution(historyEvolution);
        siteInfo.setStreetCode(streetCode);
        siteInfo.setVillageCode(villageCode);
        if (StringUtils.isNotEmpty(jurisdiction)) {
            siteInfo.setJurisdiction(jurisdiction);
        }
        if (StringUtils.isNotEmpty(picture)) {
            siteInfo.setPicture(picture);
        }
        return siteService.editSiteInfo(siteInfo);
    }

    /**
     * 删除场所
     *
     * @param siteInfoCode 场所信息编码
     * @return Return
     */
    @PostMapping("/delete")
    public Return delete(String siteInfoCode) {
        if (StringUtils.isEmpty(siteInfoCode)) {
            return new Return(Return.FAIL, "siteInfoCode 不能为空");
        }
        siteService.delete(siteInfoCode);
        return new Return(Return.SUCCESS, "删除成功");
    }
}
